<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>政策参数设置</title>
    <%= stylesheet_link_tag "/stylesheets/Multiselect.css" %>
    <%= javascript_include_tag "/javascripts/Multiselect.js" %>
    <%= javascript_include_tag "/javascripts/DDView.js" %>
  </head>
  <body>
    <div id="policy_search_form"></div>
    <div id="policy_win"></div>
    <div id="policy_grid" style="height:580px;"></div>
    <script>
        var editPolicyWindow;
        var policyDS;
        /*
         * Ext JS Library 2.1
         * Copyright(c) 2006-2008, Ext JS, LLC.
         * licensing@extjs.com
         *
         * http://extjs.com/license
         */
        Ext.onReady(function(){
            var searchAirways = new Ext.form.ComboBox({
                store: new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: '/admin/getInternalAirways'
                    }),
                    reader: new Ext.data.JsonReader({
                        root: 'root',
                        id: 'id'
                    }, ['id', 'airways_name'])
                }),
                name: 'search_airways',
                loadingText: '正在加载...',
                displayField: 'airways_name',
                valueField: 'id',
                mode: 'remote',
                triggerAction: 'all',
                hiddenName: 'airways_id',
                fieldLabel: "<span class='red'>*</span>航空公司",
                emptyText: '请选择...',
                editable: false,
                //allowBlank: false,
                width: 90,
                maxHeight: 150,
                tabIndex: 4
            });
            var searchDateBegin = new Ext.form.DateField({
                fieldLabel: '<span class="red">*</span>开始日期',
                name: 'search_date_begin',
                id: 'search_date_begin',
                format: 'Y-m-d', //格式化日期
                width: '80',
                //disabledDays: [0,6],	//不让选择周六，周日
                //disabledDaysText: "周末要休息",
                invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
                allowBlank: false,
                inputType: '2008-01-01',
                anchor: '90%',
                vtype: 'daterange',
                endDateField: 'search_date_end'
            });

            var searchDateEnd = new Ext.form.DateField({
                fieldLabel: '<span class="red">*</span>结束日期',
                name: 'search_date_end',
                id: 'search_date_end',
                format: 'Y-m-d', //格式化日期
                width: '80',
                //disabledDays: [0,6],	//不让选择周六，周日
                //disabledDaysText: "周末要休息",
                invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
                allowBlank: false,
                inputType: '2008-01-01',
                anchor: '90%',
                vtype: 'daterange',
                startDateField: 'search_date_begin'
            });

            var searchButton = new Ext.Button({
                text: '搜 索',
                handler: searchPolicy
            })
            var searchFormPanel = new Ext.form.FormPanel({
                baseCls: 'x-plain',
                labelWidth: 60,
                width: '550',
                labelAlign: 'right',
                //url: '/admin/addAirways',
                //defaultType: 'textfield',
                items: [{
                        layout: 'column',
                        border: false,
                        items: [{
                                columnWidth: .3,
                                layout: 'form',
                                border: false,
                                items: [searchAirways]
                            },{
                                columnWidth: .3,
                                layout: 'form',
                                border: false,
                                items: [searchDateBegin]
                            },{
                                columnWidth: .3,
                                layout: 'form',
                                border: false,
                                items: [searchDateEnd]
                            }, {
                                columnWidth: .1,
                                layout: 'form',
                                border: false,
                                items: [searchButton]
                            }]
                    }]
            });
            searchFormPanel.render('policy_search_form');
            function searchPolicy(){
                if (searchFormPanel.form.isValid()) {
                    policyDS.baseParams.date_begin = Ext.util.Format.date(searchDateBegin.getValue(), 'Y-m-d');
                    policyDS.baseParams.date_end = Ext.util.Format.date(searchDateEnd.getValue(), 'Y-m-d');
                    policyDS.baseParams.airways_id = searchAirways.getValue();

                    policyDS.load({
                        params: {
                            start: 0,
                            limit: 20
                        }
                    });
                    //policyDS.baseParams.date_begin = null;
                    //policyDS.baseParams.date_end = null;
                    //policyDS.baseParams.airways_id = null;
                }
            }
            var policyCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                    id: 'id',
                    header: "Identify",
                    dataIndex: 'id',
                    width: 50,
                    hidden: true
                }, {
                    header: "航空公司",
                    width: 30,
                    dataIndex: 'airways_name'
                }, {
                    header: "始发地",
                    width: 60,
                    dataIndex: 'airport_from'
                }, {
                    header: "目的地",
                    width: 170,
                    dataIndex: 'airports_to_desc'
                }, {
                    header: "航班",
                    width: 100,
                    dataIndex: 'flights'
                }, {
                    header: "舱位",
                    width: 80,
                    dataIndex: 'position'
                }, {
                    header: "类型",
                    width: 20,
                    dataIndex: 'trip_type'
                }, {
                    header: "返利",
                    width: 20,
                    dataIndex: 'rake_off'
                }, {
                    header: "开始日期",
                    width: 40,
                    dataIndex: 'date_begin',
                    renderer: Ext.util.Format.dateRenderer('Y-m-d')
                }, {
                    header: "结束日期",
                    width: 40,
                    dataIndex: 'date_end',
                    renderer: Ext.util.Format.dateRenderer('Y-m-d')
                }, {
                    header: "添加者",
                    width: 30,
                    dataIndex: 'user_name'
                }, {
                    header: "编辑日期",
                    width: 65,
                    dataIndex: 'updated_at',
                    renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
                }, {
                    header: "添加日期",
                    width: 65,
                    dataIndex: 'created_at',
                    renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
                }]);
            // second tabs built from JS
            policyDS = new Ext.data.Store({
                // load using script tags for cross domain, if the data in on the same domain as
                // this page, an HttpProxy would be better
                proxy: new Ext.data.HttpProxy({
                    url: '/admin/getAllPolicy'
                }),
                // create reader that reads the Topic records
                reader: new Ext.data.JsonReader({
                    root: 'root',
                    totalProperty: 'totalProperty',
                    id: 'id'
                }, [{
                        name: 'id'
                    }, {
                        name: 'airways_name',
                        mapping: 'airways.airways_name'
                    }, {
                        name: 'airport_from',
                        mapping:'airport_from.airport_name'
                    }, {
                        name: 'airports_to_desc'
                    }, {
                        name: 'flights'
                    }, {
                        name: 'date_begin'
                    }, {
                        name: 'date_end'
                    }, {
                        name: 'position'
                    }, {
                        name: 'trip_type'
                    }, {
                        name: 'rake_off'
                    }, {
                        name: 'user_name',
                        mapping: 'user.user_name'
                    }, {
                        name: 'created_at'
                    }, {
                        name: 'updated_at'
                    }])
            });


            /************************************************************
             * add a paging toolbar to the grid's footer
             ************************************************************/
            var myPagingToolbar = new Ext.PagingToolbar({
                pageSize: 20,
                displayInfo: true,
                displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
                emptyMsg: "没有记录",
                store: policyDS
            });

            /************************************************************
             * Define menubar now in here
             *   add and delete functions
             ************************************************************/
            var menubar = [{
                    text: '增加政策',
                    //tooltip: 'Add a new customer',
                    iconCls: 'addItem',
                    handler: function(){
                        createPolicy(policyDS);
                    }
                }, '-', {
                    text: '删除政策',
                    //tooltip: 'Remove the selected items',
                    iconCls: 'remove',
                    handler: function(){
                        // delete customer now - action
                        deletePolicy(policyDS, policyGridPanel);
                    }
                }, '-', {
                    text: '编辑政策',
                    //tooltip: '编辑政策',
                    iconCls: 'editItem',
                    handler: function(){
                        // delete costlevel now - action
                        editPolicy(policyDS, policyGridPanel);
                    }
                }];
            /************************************************************
             * define grid panel now
             ************************************************************/
            var policyGridPanel = new Ext.grid.GridPanel({
                border: false,
                el: 'policy_grid',
                ds: policyDS,
                cm: policyCM,
                autoWidth: true,
                viewConfig: {
                    forceFit: true
                },
                loadMask: true,
                iconCls: 'icon-grid',
                collapsible: true,
                animCollapse: false,
                autoWidth: true,
                //frame: true,
                title: '政策列表',
                tbar: menubar,
                bbar: myPagingToolbar
            });

            //panel render
            policyGridPanel.render();
            // trigger the data store load
            policyDS.load({
                params: {
                    start: 0,
                    limit: 20
                }
            });

            /************************************************************
             * Action - edit
             *   handle double click
             *   user select one of the item and want to update it
             ************************************************************/
            policyGridPanel.on('rowdblclick', function(policyGridPanel, rowIndex, e){
                var selectedId = policyDS.data.items[rowIndex].id;
            });

            //表单部分
            function createPolicy(){
                var airways = new Ext.form.ComboBox({
                    store: new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: '/admin/getInternalAirways'
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root',
                            id: 'id'
                        }, ['id', 'airways_name'])
                    }),
                    name: 'airways',
                    loadingText: '正在加载...',
                    displayField: 'airways_name',
                    valueField: 'id',
                    mode: 'remote',
                    triggerAction: 'all',
                    hiddenName: 'airways_id',
                    fieldLabel: "<span class='red'>*</span>航空公司",
                    emptyText: '请选择...',
                    editable: false,
                    allowBlank: false,
                    width: 130,
                    maxHeight: 150,
                    tabIndex: 4
                });

                var airportFrom = new Ext.form.ComboBox({
                    store: new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: '/admin/getAllAirportComboBox'
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root',
                            id: 'id'
                        }, ['id', 'airport_name'])
                    }),
                    name: 'airport_from',
                    loadingText: '正在加载...',
                    displayField: 'airport_name',
                    valueField: 'id',
                    mode: 'remote',
                    triggerAction: 'all',
                    hiddenName: 'airport_from_id',
                    fieldLabel: '<span class="red">*</span>始发城市',
                    emptyText: '请选择...',
                    editable: false,
                    allowBlank: false,
                    width: 130,
                    maxHeight: 150,
                    tabIndex: 4
                });

                var flights = new Ext.form.TextField({
                    fieldLabel: '适用航班',
                    //allowBlank: false,
                    name: 'flights',
                    anchor: '90%'
                });

                var rakeOff = new Ext.form.NumberField({
                    fieldLabel: '<span class="red">*</span>B政策折扣',
                    allowBlank: false,
                    name: 'rake_off',
                    anchor: '90%'
                });
                //自动出票
                var single = new Ext.form.Radio({
                    fieldLabel: '航程类型',
                    name: 'trip_type',
                    id: 'trip_type_single',
                    boxLabel: '单程',
                    checked: true,
                    inputValue: '单程'
                });
                var twice = new Ext.form.Radio({
                    fieldLabel: '往返',
                    name: 'trip_type',
                    id: 'trip_type_twice',
                    boxLabel: '往返',
                    checked: false,
                    hideLabel: true,
                    inputValue: '往返'
                });
                var dateBegin = new Ext.form.DateField({
                    fieldLabel: '<span class="red">*</span>开始日期',
                    name: 'date_begin',
                    id: 'date_begin',
                    format: 'Y-m-d', //格式化日期
                    width: '80',
                    //disabledDays: [0,6],	//不让选择周六，周日
                    //disabledDaysText: "周末要休息",
                    invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
                    allowBlank: false,
                    inputType: '2008-01-01',
                    anchor: '90%',
                    vtype: 'daterange',
                    endDateField: 'date_end'
                });

                var dateEnd = new Ext.form.DateField({
                    fieldLabel: '<span class="red">*</span>结束日期',
                    name: 'date_end',
                    id: 'date_end',
                    format: 'Y-m-d', //格式化日期
                    width: '80',
                    //disabledDays: [0,6],	//不让选择周六，周日
                    //disabledDaysText: "周末要休息",
                    invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
                    allowBlank: false,
                    inputType: '2008-01-01',
                    anchor: '90%',
                    vtype: 'daterange',
                    startDateField: 'date_begin'
                });

                var airports = new Ext.ux.ItemSelector({
                    xtype: "itemselector",
                    name: "airports",
                    imagePath: "/images/multi_select",
                    drawUpIcon: false,
                    drawDownIcon: false,
                    drawLeftIcon: true,
                    drawRightIcon: true,
                    drawTopIcon: false,
                    drawBotIcon: false,
                    fieldLabel: "<span class='red'>*</span>目的地选择",
                    dataFields: ["code", "desc"],
                    //fromData: [[123, "One Hundred Twenty Three"], ["1", "One"], ["2", "Two"], ["3", "Three"], ["4", "Four"], ["5", "Five"], ["6", "Six"], ["7", "Seven"], ["8", "Eight"], ["9", "Nine"]],
                    fromData: <%=@airports_arraystr%>,
                    toData: [],
                    msWidth: 180,
                    msHeight: 150,
                    valueField: "code",
                    displayField: "desc",
                    //imagePath:"ext-ux/multiselect",
                    //switchToFrom:true,
                    toLegend: "已经选择的城市",
                    fromLegend: "可选城市",
                    toTBar: [{
                            text: "清除",
                            handler: function(){
                                var i = policyForm.getForm().findField("airports");
                                i.reset.call(i);
                            }
                        }]
                })
                var position = new Ext.ux.ItemSelector({
                    xtype: "itemselector",
                    name: "position",
                    imagePath: "/images/multi_select",
                    drawUpIcon: false,
                    drawDownIcon: false,
                    drawLeftIcon: true,
                    drawRightIcon: true,
                    drawTopIcon: false,
                    drawBotIcon: false,
                    fieldLabel: "<span class='red'>*</span>舱位选择",
                    dataFields: ["code", "desc"],
                    //fromData: [[123, "One Hundred Twenty Three"], ["1", "One"], ["2", "Two"], ["3", "Three"], ["4", "Four"], ["5", "Five"], ["6", "Six"], ["7", "Seven"], ["8", "Eight"], ["9", "Nine"]],
                    fromData: [['A','A'],['B','B'],['C','C'],['D','D'],['E','E'],['F','F'],['G','G'],['H','H'],['I','I'],['J','J'],['K','K'],['L','L'],['M','M'],['N','N'],['O','O'],['P','P'],['Q','Q'],['R','R'],['S','S'],['T','T'],['U','U'],['V','V'],['W','W'],['X','X'],['Y','Y'],['Z','Z']],
                    toData: [],
                    msWidth: 180,
                    msHeight: 150,
                    valueField: "code",
                    displayField: "desc",
                    //imagePath:"ext-ux/multiselect",
                    //switchToFrom:true,
                    toLegend: "已经选择的舱位",
                    fromLegend: "可选舱位",
                    toTBar: [{
                            text: "清除",
                            handler: function(){
                                var i = policyForm.getForm().findField("position");
                                i.reset.call(i);
                            }
                        }]
                })
                var policyForm = new Ext.form.FormPanel({
                    labelWidth: 80,
                    width: 550,
                    frame: true,
                    labelAlign: 'right',
                    url: '/admin/addPolicy',
                    items: [{
                            layout: 'column',
                            border: false,
                            items: [{
                                    columnWidth: .5,
                                    layout: 'form',
                                    border: false,
                                    items: [airways]
                                }, {
                                    columnWidth: .5,
                                    layout: 'form',
                                    border: false,
                                    items: [airportFrom]
                                }]
                        },airports,position,flights,  {
                            layout: 'column',
                            border: false,
                            items: [{
                                    columnWidth: .5,
                                    layout: 'form',
                                    border: false,
                                    items: [rakeOff]
                                }, {
                                    columnWidth: .25,
                                    layout: 'form',
                                    border: false,
                                    items: [single]
                                }, {
                                    columnWidth: .25,
                                    layout: 'form',
                                    border: false,
                                    items: [twice]
                                }]
                        },{
                            layout: 'column',
                            border: false,
                            items: [{
                                    columnWidth: .5,
                                    layout: 'form',
                                    border: false,
                                    items: [dateBegin]
                                }, {
                                    columnWidth: .5,
                                    layout: 'form',
                                    border: false,
                                    items: [dateEnd]
                                }]
                        }]
                });

                //, flights, airwaysname, discountFrom, discountTo, dateBegin, dateEnd,isReleased,rakeOff,ticketPaymentType
                //policyForm.render("policy_form");

                // define window and show it in desktop
                var window = new Ext.Window({
                    title: '增加新政策',
                    minimizable: true,
                    maximizable: true,
                    width: 600,
                    height: 560,
                    minWidth: 600,
                    minHeight: 560,
                    layout: 'fit',
                    plain: true,
                    bodyStyle: 'padding:5px;',
                    buttonAlign: 'center',
                    items: policyForm,
                    modal: true,
                    buttons: [{
                            text: '增加',
                            handler: function(){
                                // check form value
                                if (policyForm.form.isValid()) {
                                    policyForm.form.submit({
                                        //url: '/admin/addPolicy',
                                        //params: policyForm.form.getValues(true),
                                        method: 'GET',
                                        success: function(result, response){
                                            if (response.result.info == "政策添加成功") {
                                                Ext.MessageBox.alert('提示', response.result.info);
                                                window.destroy();
                                                policyDS.load({
                                                    params: {
                                                        start: 0,
                                                        limit: 10
                                                    }
                                                });
                                            }
                                            else {
                                                Ext.MessageBox.alert('错误消息', response.result.info);
                                            }
                                        },
                                        failure: function(result, request){
                                            Ext.MessageBox.alert('错误消息', '增加政策时发生错误!');
                                        }
                                    });
                                }
                                else {
                                    Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                                }
                            }
                        }, {
                            text: '取消',
                            handler: function(){
                                window.destroy();
                            }
                        }]
                });
                window.show();
            }

            //表单部分
            function editPolicy(policyDS, policyGridPanel){
                var m = policyGridPanel.getSelections();
                if (m.length > 0 && m.length == 1) {
                    var selectedId = m[0].get("id");
                    // define window and show it in desktop
                    if (!editPolicyWindow) {
                        editPolicyWindow = new Ext.Window({
                            title: '编辑政策',
                            minimizable: true,
                            maximizable: true,
                            width: 600,
                            height: 500,
                            minWidth: 600,
                            minHeight: 500,
                            layout: 'fit',
                            el: 'policy_win',
                            plain: true,
                            bodyStyle: 'padding:5px;',
                            buttonAlign: 'center',
                            //items: policyForm,
                            modal: true,
                            autoLoad: {
                                url: '/admin/editPolicy?id='+selectedId, nocache: true,
                                scripts: true
                            }
                        });
                    }else{
                        var url = '/admin/editPolicy?id='+selectedId;
                        editPolicyWindow.getUpdater().update(url);
                    }
                    editPolicyWindow.show();
                }else {
                    Ext.MessageBox.alert('错误提示', '要编辑政策时，请至少选择一个政策');
                }
            }
        });
    </script>
  </body>
</html>
